package com.campus.controller.admin;

import com.campus.exception.AgeIllegalException;
import com.campus.pojo.dto.StudentDTO;
import com.campus.pojo.dto.StudentPageQueryDTO;
import com.campus.pojo.vo.StudentVO;
import com.campus.result.PageResult;
import com.campus.result.Result;
import com.campus.service.StudentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Tag(name = "学生管理", description = "学生管理")
@RestController
@RequestMapping("/admin/student")
@RequiredArgsConstructor
@CrossOrigin //允许跨域请求
public class StudentController { 

    private final StudentService studentService;

    /**
     * 分页查询学生列表
     * @param studentPageQueryDTO
     * @return
     */
    @Operation(
            summary = "分页查询学生列表",
            description = "分页查询学生列表"
    )
    @GetMapping
    public Result page(StudentPageQueryDTO studentPageQueryDTO){
        log.info("分页查询：{}", studentPageQueryDTO);
        PageResult pageResult = studentService.page(studentPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据学生id查询学生信息
     * @param id
     * @return
     */
    @Operation(summary = "根据学生id查询学生信息")
    @Parameter(name = "id", description = "学生id")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id){
        log.info("根据学生id查询学生信息：{}", id);
        StudentVO studentVO = studentService.getById(id);
        return Result.success(studentVO);
    }

    /**
     * 根据id删除学生信息
     * @param id
     * @return
     */
    @Operation(summary = "根据id删除学生信息")
    @Parameter(name = "id", description = "学生id")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        log.info("删除学生信息：{}", id);
        studentService.delById(id);
        return Result.success();
    }

    /**
     * 根据id更新学生信息
     * @param studentDTO
     * @return
     * @throws AgeIllegalException
     */
    @Operation(summary = "更新学生信息")
    @Parameter(name = "studentDTO", description = "学生信息")
    @PutMapping
    public Result update(@RequestBody StudentDTO studentDTO) throws AgeIllegalException {
        log.info("更新学生信息：{}", studentDTO);
        studentService.update(studentDTO);
        return Result.success();
    }

    /**
     * 新增学生信息
     * @param studentDTO
     * @return
     */
    @Operation(summary = "新增学生信息")
    @Parameter(name = "studentDTO", description = "学生信息")
    @PostMapping
    public Result addStudent(@RequestBody StudentDTO studentDTO){
        log.info("添加学生信息：{}", studentDTO);
        studentService.addStudent(studentDTO);
        return Result.success();
    }
}
